#pragma once

#include "PCH.h"
#include "Material.h"
#include "Vector.h"
//#include "Raytrace.h"

class CRay;

class CRenderableObject
{
public:
	bool NegativeObject;

	CMaterial Material;

	bool Selected;

	enum ObjectType{
		SPHERE,
		TRIANGLE,
		PLANE,
		METAOBJECT
	};

	virtual double HitDistance(const CRay &Ray)=0;
	virtual void GetNormal(const CVector &ImpactPoint,CRay &NormalRay)=0;
	virtual void CausticRayLister(const CVector &Source,std::vector<CRay> &RayList)=0;
	virtual bool OverlapBox(const CVector &boxcenter,const CVector &boxhalfsize)=0;
	virtual ObjectType GetType()=0;
	virtual void	GetMaterial(const CVector &ImpactPoint, CMaterial &_Material){_Material=Material;}

};


class CTriangleObject : public CRenderableObject
{
public:
	CVector Point1;
	CVector Point2;
	CVector Point3;

	virtual double	HitDistance(const CRay &Ray);
	virtual void	GetNormal(const CVector &ImpactPoint,CRay &NormalRay);
	virtual void	CausticRayLister(const CVector &Source,std::vector<CRay> &RayList);
	virtual bool	OverlapBox(const CVector &boxcenter,const CVector &boxhalfsize);
	virtual ObjectType GetType(){return TRIANGLE;}
	double	Surface () const;
	double	SolidAngleOnSphere (const CVector ProjectionPoint) const;
	CVector GetBarycenter() const;
	bool	LineToTriangleHit(const CRay &Ray, CVector & Impact);
private:
	bool	SameSide(CVector &TestPoint,CVector &RefPoint,CVector &P1,CVector &P2);
	bool	IsPointInTriangle(CVector &TestPoint);
	void	FindMinMax(double X0,double X1,double X2,double & Min,double & Max);
	bool	PlaneBoxOverlap(CVector Normal, CVector Vert, CVector MaxBox);
	bool	triBoxOverlap(const CVector &boxcenter,const CVector &boxhalfsize,const CVector triverts[3]);
};


class CSphereObject : public CRenderableObject
{
public:
	CVector	Position;
	double	Radius;
	virtual double	HitDistance(const CRay &Ray);
	virtual void	GetNormal(const CVector &ImpactPoint,CRay &NormalRay);
	virtual void	CausticRayLister(const CVector &Source,std::vector<CRay> &RayList);
	virtual bool	OverlapBox(const CVector &boxcenter,const CVector &boxhalfsize);
	virtual ObjectType GetType(){return SPHERE;}
};

class CPlaneObject : public CRenderableObject
{
public:
	CVector	NormalVector;
	double	Offset;
	double	Radius;
	virtual double	HitDistance(const CRay &Ray);
	virtual void	GetNormal(const CVector &ImpactPoint,CRay &NormalRay);
	virtual void	CausticRayLister(const CVector &Source,std::vector<CRay> &RayList);
	virtual bool	OverlapBox(const CVector &boxcenter,const CVector &boxhalfsize);
	virtual ObjectType GetType(){return PLANE;}
};
